<template>
  <div class="time-select">
    <van-cell
      is-link
      :value="showTime"
      @click="selectStartTime"
    >
      <template #title>
        <span class="custom-title">{{ title }}</span>
      </template>
    </van-cell>

    <van-action-sheet v-model="showTimePicker">
      <!-- :title="timePickerType == 'start' ? '开始时间' : '结束时间'" -->
      <van-datetime-picker
        v-model="currentDate"
        type="datetime"
        @confirm="timeSelectConfirm"
        @cancel="showTimePicker = false"
      />
    </van-action-sheet>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  name: "TimeSelect",
  props: {
    value: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "",
    },
  },
  model: {
    prop: "value",
    event: "change",
  },
  watch: {
    value(val) {
      if (val) {
        this.currentDate = new Date(this.$options.filters.timeDate(val));
      }
    },
  },
  computed: {
    showTime() {
      if (!this.value) return "请选择";
      return this.$options.filters.timeDate(this.value)
    }
  },
  data() {
    return {
      currentDate: new Date(),
      showTimePicker: false,
    };
  },
  methods: {
    selectStartTime() {
      this.showTimePicker = true;
    },
    timeSelectConfirm(val) {
      let value = dayjs(val).format("YYYYMMDDHHmmss");
      this.$emit("change", value);
      this.showTimePicker = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.time-select {
}
</style>
